package top.aprillie.mamabike.common.rest;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import top.aprillie.mamabike.cache.CommonCacheUtil;
import top.aprillie.mamabike.common.constants.Constants;
import top.aprillie.mamabike.user.entity.UserElement;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author: illure
 * @Description:
 * @Date: Created in 16:42 2018/2/11
 * @Modified By:
 */
@Slf4j
public class BaseController {

    @Autowired
    private CommonCacheUtil commonCacheUtil;

    protected UserElement getCurrentUser() {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
        String token = request.getHeader(Constants.REQUEST_TOKEN_KEY);
        if (!StringUtils.isBlank(token)) {
            try {
                UserElement ue = commonCacheUtil.getUserByToken(token);
                return ue;
            } catch (Exception e) {
                log.error("Fail to get user by token", e);
                throw e;
            }
        }
        return null;
    }

    protected String getIpFromRequest(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip.equals("0:0:0:0:0:0:0:1") ? "127.0.0.1" : ip;
    }
}
